new Vue({
    el: '#app',
    data() {
        const Pass_Word = (rule, value, callback) => {
            if (validatePassword(this.form.Pass_Word)) {
                callback(new Error(validatePassword(this.form.Pass_Word)));
            } else {
                callback();
            }
        }
        const User_Number = (rule, value, callback) => {
            if (!validatemobile(this.form.User_Number)) {
                callback(new Error('请输入有效手机号'));
            } else {
                callback();
            }
        }
        return {
            form: {
                ID_Number: '',
                ID_Name: '',
                ID_Front: '',
                ID_Back: '',
                Phone: '',
                Validity_EndDate: '',
                ID_Address: '',
                ProvinceNumber: '',
                CityNumber: '',
                CountyNumber: '',
                TownNumber: '',
                VillageNumber: '',
                value1: [],
                town: [],//市区
                value2: [],
            },
            land: [{
                Traceability_Land_SerialNumber: '',
                ID_Number: '',
                Land_Number: '',
                ProvinceNumber: '',
                CityNumber: '',
                CountyNumber: '',
                TownNumber: '',
                VillageNumber: '',
                Located: '',
                Planting_Area: '',
                Category_SerialNumber: '',
                Land_Certificate: '',
                longitude:'',
                latitude:'',
                TownJSON:'',
                value1: [],
                town: [],//市区
                value2: [],
            }],
            rules: {

            },
            iconLoading: false,
            city: [],//城市
            value: [],
            category: [],
            address: '',
            loading: false,
        }
    },
    mounted() {
        this.getData();
    },
    watch: {
        'form.ID_Number'(val) {
            this.land.forEach(item => {
                item.ID_Number = val
            })
        },
        'form.ID_Front'(Data) {
            const that = this;
            that.iconLoading = true;
            $.ajax({
                url: `${BASE_URL1}/parseIDCardAction`,
                type: 'POST',
                data: { Data },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        that.form.ID_Number = Result.ID_Number;
                        that.form.ID_Name = Result.name;
                        let reg = /.+?(省|市|自治区|自治州|县|区)/g;
                        let arr = Result.address.match(reg);
                        let obj1 = that.city.filter(i=>i.children&&i.children.some(x=>x.label==arr[0]))[0];
                        let obj2 = obj1.children.filter(i=>i.label == arr[0])[0]
                        let obj3 = obj2.children.filter(i=>i.label == arr[1])[0]
                        let arr1 = [];
                        if(obj1){
                            arr1 = [obj1.value]
                            if(obj2){
                                arr1 = [...arr1,obj2.value]
                                if(obj3){
                                    arr1 = [...arr1,obj3.value]
                                }
                            }
                        }
                        that.form.value1 = arr1
                    }
                },
                complete: function () {
                    that.iconLoading = false;
                },
                dataType: "json"
            });
        },
        'form.VillageNumber'(val){
            if (val) {
                // let str = this.form.
            }
        }
    },
    methods: {
        getData() {
            const that = this;
            $.ajax({
                url: absolutepath() + "utils/city.json",
                type: "GET",
                success: function (data) {
                    that.city = data
                },
                dataType: "json"
            })
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'business_category',
                    Condition: "Row_Status = '0'",
                },
                success: function (Result) {
                    that.category = Result
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        changeCity(arr, row) {
            //初始化
            row.ProvinceNumber = '';
            row.CityNumber = '';
            row.CountyNumber = '';
            row.TownNumber = '';
            row.VillageNumber = '';
            row.value2 = [];
            row.town = [];
            if (arr.length > 0) {
                row.ProvinceNumber = arr[0];
                row.CityNumber = arr[1];
                row.CountyNumber = arr[2];
                $.ajax({
                    url: `${BASE_URL1}/getTownSelector?CountyNumber=${arr[2]}`,
                    type: 'GET',
                    success: function (Result) {
                        row.town = Result;
                    },
                    dataType: "json"
                });
            }
        },
        changeTown(arr, row) {
            row.TownNumber = arr[0] ? arr[0] : '';
            row.VillageNumber = arr[1] ? arr[1] : '';
        },
        handleLand(Data,row){
            $.ajax({
                url: `${BASE_URL1}/landCertificateAction`,
                type: 'POST',
                data: { Data },
                success: function (Result) {
                    console.log(Result);
                    if (Result.Status=="success") {
                        row.Located = Result.located
                        row.Land_Number = Result.landNumber
                        row.Planting_Area = Result.UsufructArea
                    }
                },
                dataType: "json"
            });
        },
        add_land() {
            this.land.push({
                Traceability_Land_SerialNumber: '',
                ID_Number: this.form.ID_Number,
                Land_Number: '',
                ProvinceNumber: '',
                CityNumber: '',
                CountyNumber: '',
                TownNumber: '',
                VillageNumber: '',
                Located: '',
                Planting_Area: '',
                Category_SerialNumber: '',
                Land_Certificate: '',
                TownJSON:'',
                value1: [],
                town: [],//市区
                value2: [],
            })
        },
        remove_land() {
            if (this.land.length > 1) {
                this.land.pop()
            } else {
                this.$message({
                    message: '不能在删除了',
                    type: 'warning'
                })
            }
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    that.loading = true
                    let business_traceability_land = [];
                    let business_traceability_farmer = [{
                        ID_Number: that.form.ID_Number,
                        ID_Name: that.form.ID_Name,
                        ID_Front: that.form.ID_Front,
                        ID_Back: that.form.ID_Back,
                        Phone: that.form.Phone,
                        Validity_EndDate: that.form.Validity_EndDate,
                        ID_Address: that.form.ID_Address,
                        ProvinceNumber: that.form.ProvinceNumber,
                        CityNumber: that.form.CityNumber,
                        CountyNumber: that.form.CountyNumber,
                        TownNumber: that.form.TownNumber,
                        VillageNumber: that.form.VillageNumber,
                        // TownJSON:JSON.stringify(that.form.town)
                        TownJSON:'[]',
                    }]
                    that.land.forEach(item => {
                        business_traceability_land.push({
                            Traceability_Land_SerialNumber: item.Traceability_Land_SerialNumber,
                            ID_Number: item.ID_Number,
                            Land_Number: item.Land_Number,
                            ProvinceNumber: item.ProvinceNumber,
                            CityNumber: item.CityNumber,
                            CountyNumber: item.CountyNumber,
                            TownNumber: item.TownNumber,
                            VillageNumber: item.VillageNumber,
                            Located: item.Located,
                            Planting_Area: item.Planting_Area,
                            Category_SerialNumber: item.Category_SerialNumber,
                            Land_Certificate: item.Land_Certificate,
                            TownJSON:'[]',
                            // TownJSON:JSON.stringify(item.town)
                        })
                    })
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { business_traceability_farmer, business_traceability_land } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})